package com.demo.sso.sdk.model;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.Serializable;

/**
 * SSO API响应模型
 * 
 * @author SpringCloud团队
 * @version 1.0.0
 * @since 2025-01-01
 */
public class SsoApiResponse<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 响应码（200-成功，其他-失败）
     */
    @JsonProperty("code")
    private Integer code;

    /**
     * 响应消息
     */
    @JsonProperty("msg")
    private String msg;

    /**
     * 响应数据
     */
    @JsonProperty("data")
    private T data;

    /**
     * 响应时间戳
     */
    @JsonProperty("timestamp")
    private Long timestamp;

    public SsoApiResponse() {
        this.timestamp = System.currentTimeMillis();
    }

    public SsoApiResponse(Integer code, String msg) {
        this();
        this.code = code;
        this.msg = msg;
    }

    public SsoApiResponse(Integer code, String msg, T data) {
        this(code, msg);
        this.data = data;
    }

    /**
     * 判断响应是否成功
     * 
     * @return true-成功，false-失败
     */
    public boolean isSuccess() {
        return code != null && code == 200;
    }

    /**
     * 创建成功响应
     * 
     * @param data 响应数据
     * @param <T> 数据类型
     * @return 成功响应
     */
    public static <T> SsoApiResponse<T> success(T data) {
        return new SsoApiResponse<>(200, "success", data);
    }

    /**
     * 创建成功响应
     * 
     * @param msg 响应消息
     * @param data 响应数据
     * @param <T> 数据类型
     * @return 成功响应
     */
    public static <T> SsoApiResponse<T> success(String msg, T data) {
        return new SsoApiResponse<>(200, msg, data);
    }

    /**
     * 创建失败响应
     * 
     * @param code 错误码
     * @param msg 错误消息
     * @param <T> 数据类型
     * @return 失败响应
     */
    public static <T> SsoApiResponse<T> error(Integer code, String msg) {
        return new SsoApiResponse<>(code, msg);
    }

    /**
     * 创建失败响应
     * 
     * @param msg 错误消息
     * @param <T> 数据类型
     * @return 失败响应
     */
    public static <T> SsoApiResponse<T> error(String msg) {
        return new SsoApiResponse<>(500, msg);
    }

    // Getters and Setters

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    @Override
    public String toString() {
        return "SsoApiResponse{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", timestamp=" + timestamp +
                '}';
    }
}